<template>
  <div class="index-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        round
        to="/search"
        class="search-btn"
        slot="title"
        icon="search"
        type="info"
        size="small"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 导航栏 / -->

    <!-- 频道列表 -->
    <van-tabs
      class="channel-tabs"
      v-model="active"
      animated
      swipeable
      :swipe-threshold="4"
      title-active-color="#333"
    >
      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isShowEdit = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
      <van-tab :title="item.name" v-for="item in showList" :key="item.id">
        <ArticleList :item="item"></ArticleList>
      </van-tab>
    </van-tabs>
    <!-- 频道列表 /-->
    <!-- 频道弹出层 -->
    <van-popup
      v-model="isShowEdit"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
      @close="changeEditStateWhenClose"
    >
      <!-- 使用ref标记该组件，当弹出层关闭时调用方法修改子组件状态数据 -->
      <ChannelEdit
        ref="channelEdit"
        :myChannel="showList"
        :active="active"
        @delOrChoose="delOrChoose"
        @addRecommendChannel="addRecommendChannel"
      ></ChannelEdit>
    </van-popup>
    <!-- 频道弹出层 /-->
  </div>
</template>

<script>
// 引入文章列表组件
import ArticleList from './components/ArticleList'
// 引入频道编辑组件
import ChannelEdit from './components/channelEdit'
import {
  getUserChannels,
  deleteUserChannel,
  addUserChannel
} from '@/api/index.js'
import { setItem, getItem } from '@/utils/storage.js'
export default {
  name: 'index',
  components: { ArticleList, ChannelEdit },
  data () {
    return {
      active: 0,
      showList: [],
      channelsList: [],
      isShowEdit: false,
      localChannel: getItem('localChannel')
    }
  },
  methods: {
    async loadChannel () {
      // 如果用户未登录并且本地有频道数据就不发请求用户数据
      if (!this.$store.state.user && this.localChannel) {
        this.showList = this.localChannel
        return
      }
      try {
        const { data: res } = await getUserChannels()
        this.showList = res.data.channels
        if (!this.$store.state.user) {
          setItem('localChannel', res.data.channels)
        }
      } catch (error) {
        this.$toast.fail('获取频道失败')
      }
    },
    async delOrChoose (id, index, delFlag) {
      // 是否执行删除
      if (delFlag) {
        // 如果登录了就发请求修改后台数据
        if (this.$store.state.user) {
          try {
            await deleteUserChannel(id)
            // do del(无论是否登陆都要修改本地展示数据)
            this.showList.splice(index, 1)
            this.$toast.success('删除成功')
          } catch (error) {
            return this.$toast.fail('服务器繁忙,请稍后再试')
          }
          // 如果没有登录就操作本地数据
        } else {
          // do del(无论是否登陆都要修改本地展示数据)
          this.showList.splice(index, 1)
          setItem('localChannel', this.showList)
        }
        if (index <= this.active) {
          this.active -= 1
        }
      } else {
        // do choose
        this.active = index
      }
    },
    async addRecommendChannel (channel) {
      // 登录状态下发请求给后台增加频道
      if (this.$store.state.user) {
        try {
          await addUserChannel({
            id: channel.id, // 频道 id
            seq: this.showList.length // 序号
          })
          this.showList.push(channel)
          this.$toast.success('添加成功')
        } catch (error) {
          return this.$toast.fail('服务器繁忙,请稍后再试')
        }
        // 如果没有登录就操作本地数据
      } else {
        // add channel
        this.showList.push(channel)
        setItem('localChannel', this.showList)
      }
      // add channel
    },
    changeEditStateWhenClose () {
      // change sonComponent's isEdit(in data) when popupCom closing
      this.$refs.channelEdit.isEdit = false
    }
  },
  created () {
    this.loadChannel()
  }
}
</script>

<style scoped lang="less" >
.index-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    font-size: 28px;
    .van-icon {
      color: #fff;
      font-size: 32px;
    }
  }
  /deep/.channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      right: 0;
      left: 0;
      height: 82px;
    }
    .van-tab {
      font-size: 30px;
      color: #777;
      min-width: 200px;
      border-right: 1px solid #edeff3;
    }
    .van-tabs__nav {
      padding-bottom: 0px;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      bottom: 8px;
      background-color: #3296fa;
    }
    // 3.汉堡按钮 ---------------------------------
    // 3.1 汉堡按钮的占位符
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    // 3.2 汉堡按钮样式
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: rgba(255, 255, 255, 0.902);
      i.iconfont {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 58px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
